package com.imti.mapper;

import com.imti.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 */
public interface UserMapper {

    /**
     * 注册用户信息
     * @param user
     * @return
     */
    @Insert("insert into t_user (username,password,role,create_time) values(#{username},#{password},#{role},CURRENT_TIMESTAMP())")
    int registed(User user);

    /**
     * 验证用户信息
     * @param s
     * @return
     */
    @Select("SELECT count(*) FROM `t_user` where username = #{username}")
    int validname(String s);

    /**
     * 验证登录
     * @param username
     * @param md5
     * @return
     */
    @Select("select * from t_user where username = #{username} and password = #{md5}")
    @ResultMap("userAllMapper")
    User verifyLogin(String username, String md5);

    /**
     * 查询列表信息
     * @param user
     * @return
     */
    @Select("select * from t_user")
    @Results(id = "userAllMapper",value = {
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "lock_flag",property = "lockFlag")
    })
    List<User> findAllUser(User user);

    /**
     * 添加信息
     * @param user
     * @return
     */
    @ResultMap("userAllMapper")
    @Insert("insert into t_user (img,username,password,phone,role,lock_flag)values(#{img},#{username},#{password},#{phone},#{role},#{lockFlag})")
    int addUserById(User user);

    /**
     * 验证手机信息
     * @param p
     * @return
     */
    @Select("SELECT count(*) FROM `t_user` where phone = #{phone} ")
    int vPhone(String p);

    /**
     * 修改信息
     * @param user
     * @return
     */
    @Update("UPDATE t_user SET img = #{img},username = #{username}, phone = #{phone}, lock_flag = #{lockFlag} WHERE id = #{id}")
    Integer UpdateUser(User user);

    /**
     * 根据id查询所有信息
     * @param id
     *
     * @return
     */
    @Select("select * from t_user where id = #{id}")
    User findUserById(Integer id);

    @Update("update t_user set password = #{password} where id = #{id}")
    boolean resPassword(String password,Integer id);

    @Select("SELECT * FROM t_user WHERE role =1")
    @ResultMap("userAllMapper")
    List<User> selectAllStu();

    @Update("UPDATE user_clazz SET clazz_id = #{id} WHERE id = #{selectedId}")
    void updateStuClaz(String selectedId, Integer id);

    @Select("select count(*) from t_user where username=#{name}")
    public int getUserName(String name);

    @Select("<script>SELECT count(*) FROM `t_user` WHERE 1=1" +
            "<if test=\"username!=null and username!=''\"> AND `username` LIKE CONCAT('%',#{username},'%')</if>" +
            "<if test=\"phone!=null and phone!=''\"> AND `phone`=#{phone}</if>" +
            "<if test=\"lockFlag!=null and lockFlag!=''\">  AND `lock_flag`=#{lockFlag}</if></script>")
    public int getUserCount(User u);
    /**
     * 获取所有用户信息
     * @return
     */
    @ResultMap("userAllMapper")
    // @SelectProvider(type =UserProvid.class,method ="findAllUser" )
    @Select("<script>SELECT * FROM `t_user` WHERE 1=1" +
            "<if test=\"u.username!=null and u.username!=''\"> AND `username` LIKE CONCAT('%',#{u.username},'%')</if>" +
            "<if test=\"u.phone!=null and u.phone!=''\"> AND `phone`=#{u.phone}</if>" +
            "<if test=\"u.lockFlag!=null and u.lockFlag!=''\">  AND `lock_flag`=#{u.lockFlag}</if>" +
            " limit #{p.pageIndex},#{p.pageLimit}</script>")
    public List<User> findAll(Map map);

    @Select("select count(*) from t_user")
    int selectUserCount();


    class UserProvid{//内部类

        /**
         *
         * 通过方法实现动态sql查询用户信息
         * @param user
         * @return
         */
        public String  findAll(User user){
            StringBuilder sb=new StringBuilder();
            sb.append("SELECT * FROM `t_user` WHERE 1=1");
            if(user.getUsername()!=null && !"".equals(user.getUsername())){
                sb.append(" AND `username` LIKE CONCAT('%',#{username},'%')");
            }
            if(user.getPhone()!=null && !"".equals(user.getPhone())){
                sb.append(" AND `phone`=#{phone}");
            }
            if(user.getLockFlag()!=null && !"".equals(user.getLockFlag())){
                sb.append(" AND `lock_flag`=#{lockFlag}");
            }
            return sb.toString();
        }
    }



}
